package com.bawei.module_sign

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.module_sign.adapter.SignAdapter
import com.bawei.module_sign.databinding.ActivitySignBinding
import com.bawei.module_sign.model.SignModelItem
import com.bawei.module_sign.ui.SignViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch


@AndroidEntryPoint
@Route(path = Router.Ui.SignActivity)
class SignActivity : BaseActivity<SignViewModel, ActivitySignBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_sign
    override val mViewModel: SignViewModel by viewModels()
    private val mSignAdapter by lazy { SignAdapter() }
    private val mSignList = mutableListOf<SignModelItem>()
    override fun initView() {
        mBinding.rvSign.apply {
            adapter = mSignAdapter
            layoutManager = LinearLayoutManager(this@SignActivity)
        }
    }

    override fun initListener() {
        mViewModel.getSign(1833)
        // 绑定 RadioGroup 监听
        mBinding.filterRadioGroup.setOnCheckedChangeListener { group, checkedId ->
            // 根据选中的 RadioButton ID，确定筛选类型
            val filterType = when (checkedId) {
                R.id.rb_all -> "全部"
                R.id.rb_normal -> "正常"
                R.id.rb_late -> "迟到"
                R.id.rb_leave_early -> "早退"
                R.id.rb_absence -> "缺勤"
                else -> "全部"
            }
            // 筛选数据并刷新列表
            filterAndRefreshList(filterType)
        }
        // 初始化调用，保证默认选中的“正常”能正确筛选
        filterAndRefreshList("正常")
        // 原有代码：mViewModel.getSign(userId = 0)
    }
    // 筛选数据并刷新列表的方法
    private fun filterAndRefreshList(filterType: String) {
        // 从 ViewModel 获取原始数据（假设 mViewModel.mSignListData 是数据源）
        val originalList = mViewModel.mSignListData
        // 根据 filterType 筛选数据
        val filteredList = originalList.filter { item ->
            when (filterType) {
                "全部" -> true
                else -> item.status == filterType
            }
        }
        // 提交筛选后的数据给 Adapter，触发列表刷新
        mSignAdapter.submitList(filteredList)
    }

    override fun initData() {
        lifecycleScope.launch {
            mViewModel.mSignState.collect{
                mSignAdapter.submitList(mViewModel.mSignListData)
                // 初始化筛选（比如默认选中“正常”）
                filterAndRefreshList("正常")
            }
        }
    }

}